package com.turing.data.scripts.yhlc.filter

import com.turing.common.util.FileUtils
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils

/**
 * @description: 银行理财 - 理财产品代码
 * @author: mazhishen
 */

class ProductCode_30248_46_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){
        def REGEX_BACKUP_1 = "产品代码"
        def REGEX_BACKUP_2 = "基金代码"
        def REGEX_BACKUP_3 = ""
//        def REGEX_BACKUP_4 = "期末份额"

        List< TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        def set = new HashSet()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                //往前10个字符搜索
                while( expandOffset < 20 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,start)
                    if( expandString =~ REGEX_BACKUP_1
                        || expandString =~ REGEX_BACKUP_2
                        || expandString =~ REGEX_BACKUP_3
                    )
                    {
                        def expand = 0
                        while( expand < 15 && end + expand < rawText.length() ){
                            def expandStr = rawText.substring(end,end+expand)
                            if(expandStr.endsWith("信托划")){
                                def JSONTextMap = dependParams.JSONTextMap
                                def fileTextStr = ""
                                fileTextStr = JSONTextMap.get(childTaskSn)
                                def startOffset = 0
                                def endOffset = expand
                                TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                                objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)
                                trueValueSet.clear()
                                trueValueSet.add(objectNew)
                                return trueValueSet
                            }
	            expand ++
                        }
                        trueValueSet.add(drawResultVO)
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }

        TaskElementVO.DrawResultVO defaultDrawResultVO = new TaskElementVO.DrawResultVO()
        defaultDrawResultVO.text = ""
        trueValueSet.add(defaultDrawResultVO)
        return  trueValueSet

    }

}
